package com.flexcloudsdk.users;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.users.UserServiceFailureException;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

@SuppressWarnings("unused")
public class CloudUser 
{
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(CloudUser.class.getName());
	private User cloudUser = null;
	private UserService cloudUserService;
	private String sourceClass;
	private String  sourceMethod;
	
	public CloudUser() {
		log.info("CloudUser has been created by BlazeDS!");
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		sourceClass = CloudUser.class.getName();
		
		cloudUserService = UserServiceFactory.getUserService();
	}
	
	@Override
	public String toString() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return cloudUser.toString();
	}
	
	public User getGoogleUser() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return cloudUser;
	}
	
	public int compareTo(CloudUser user) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		int value = 0;
		if (user == null)
			 value = -1;
		else if (cloudUser == null)
			value = -1;
		else if (cloudUser != null &&  user.getGoogleUser().compareTo(cloudUser) == 0)
			value = 0;
		else if (cloudUser != null && user.getGoogleUser().compareTo(cloudUser) == -1)
			value = -1;
		else if (cloudUser != null && user.getGoogleUser().compareTo(cloudUser) == 1)
			value = 1;
		else  {
			value = -1;
		}
		return value;
	}
	
	public String getAuthDomain() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String authDomain = "";
		if (cloudUser != null) {
			authDomain =  cloudUser.getAuthDomain();
		} else { 
			String errorValue = "User is invalid in call to getAuthDomain";
			log.warning(errorValue);
			authDomain = errorValue;
		}	
		return authDomain;
	}
	
	public String getEmail() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		
		String email = "";
		if (cloudUser != null) {
			email = cloudUser.getEmail();
		} else {
			String errorValue = "User is invalid in call to getEmail";
			log.warning(errorValue);
			email = errorValue;
		}
		return email;
	}
	
	public String getFederatedIdentity() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String federatedIdentity = "";
		if (cloudUser != null) {
			federatedIdentity = cloudUser.getFederatedIdentity();
		} else {
			String errorValue = "User is invalid in call to getFederatedIdentity";
			log.warning(errorValue);
			federatedIdentity = errorValue;
		}
		return federatedIdentity;
	}
	
	public String getNickname() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String nickname = "";
		if (cloudUser != null) {
			nickname = cloudUser.getNickname();
		} else {
			String errorValue = "User is invalid in call to getNickname";
			log.warning(errorValue);
			nickname = errorValue;
		}
		return nickname;
	}
	
	public String getUserId() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		
		String userId = "";
		if (cloudUser != null) {
			userId = cloudUser.getUserId();
		} else {
			String errorValue = "User is invalid in call to getUserId";
			log.warning(errorValue);
			userId = errorValue;
		}	
		return userId;
	}
	
	public CloudUser createNewUser(String email, String authDomain) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		
		cloudUser = new User(email, authDomain);
		return this;
	}
	
	public CloudUser createNewUser(String email, String authDomain, String userId) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		
		cloudUser = new User(email, authDomain, userId);
		return this;
	}
	
	public CloudUser createNewUser(String email, String authDomain, String userId, String federatedIdentity) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		cloudUser = new User(email, authDomain, userId, federatedIdentity);
		return this;
	}
	
	public CloudUser  getCurrentUser() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		cloudUser = cloudUserService.getCurrentUser();
		return this;
	}
	
	public boolean isUserAdmin() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		Boolean flag = false;
		try
		{
			flag = cloudUserService.isUserAdmin();
		}
		catch (IllegalStateException exception) {
			flag = false;
			log.severe("IllegalStateException in call to isUserAdmin");
			log.severe(exception.getStackTrace().toString());
		} catch (Exception exception) {
			flag = false;
			log.severe(exception.getMessage());
			log.severe(exception.getStackTrace().toString());
		}
		return flag;
	}
	
	public boolean isUserLoggedIn() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return cloudUserService.isUserLoggedIn();
	}

	public String createLoginURL(String  destinationURL) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		String url = "";
		try {
			url = cloudUserService.createLoginURL(destinationURL); 
		}
		catch (IllegalArgumentException exception) {
			url = "";
			log.warning("destinationURL is invalid in call to createLoginURL");
  	        log.severe(exception.getMessage());
      	} catch (Exception exception) {
      		url = "";
			log.warning("destinationURL is invalid in call to createLoginURL");
	        log.severe(exception.getMessage());
	  	}
		return  url;
	}
	
	public String createLoginURL(String destinationURL, String authDomain) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String url = "";
		if (cloudUserService == null) 
			log.warning("userService is invalid in call to createLoginURL(destinationURL,authDomain) ");
		else 
			url =  cloudUserService.createLoginURL(destinationURL, authDomain);
		return url;
	}
	
	public String createLoginURL(String destinationURL, String authDomain, String federatedIdentity, Set<String> attributesRequest) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		String url = "";
		try {
			if (cloudUserService == null) 
				log.warning("userService is invalid in call to createLoginURL(destinationURL,authDomain,federatedIdentity, attributesRequest);");
			else 
				url = cloudUserService.createLoginURL(destinationURL, authDomain, federatedIdentity, attributesRequest);
		} catch (IllegalArgumentException exception) {
			log.severe("destinationURL " + destinationURL + "  is invalid in call to createLogoutURL(long form);");
	        log.severe(exception.getMessage());
	    } catch (Exception exception) {
			log.severe("destinationURL " + destinationURL + "  is invalid in call to createLogoutURL(long form);");
	        log.severe(exception.getMessage());
	    }
		return url;
	}
	
	public String createLogoutURL(String destinationURL) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		String url = "";
		try {
			if (cloudUserService == null) 
				log.warning("userService is invalid in call to createLogoutURL(destinationURL);");
			else 
				url = cloudUserService.createLogoutURL(destinationURL);
		} catch (IllegalArgumentException exception) {
			log.severe("destinationURL " + destinationURL + "  is invalid in call to createLogoutURL(destinationURL);");
		    log.severe(exception.getMessage());
		} catch (Exception exception) {
		    log.severe("destinationURL " + destinationURL + "  is invalid in call to createLogoutURL(destinationURL);");
            log.severe(exception.getMessage());
       }
		return url;
	}
	
	public String createLogoutURL(String destinationURL, String authDomain) {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		String url = "";
		try  {
			if (cloudUserService == null) 
				log.warning("userService is invalid in call to createLogoutURL(destinationURL,authDomain);");
			else 
				url = cloudUserService.createLogoutURL(destinationURL, authDomain);
		} catch (IllegalArgumentException exception) {
			log.severe("destinationURL " + destinationURL + "  is invalid in call to createLogoutURL(destinationURL, authDomain);");
	        log.severe(exception.getMessage());
	  	} catch (Exception exception) {
			log.severe("destinationURL " + destinationURL + "  is invalid in call to createLogoutURL(destinationURL, authDomain);");
	        log.severe(exception.getMessage());
	  	}
		return url;
	}

}
